<template>
  <div v-if="loginType ==null" class="pfull body"  id="mainvue" v-cloak>
    <normal_login></normal_login>
  </div>
  <div v-else-if="loginType == 'dingtalk'">
    <login-dingtalk style="display: flex; align-items: center;" :logo="this.key"></login-dingtalk>
  </div>
  <div v-else-if="loginType == 'workWeixinMy'">
<!--    <login-work-weixin-my></login-work-weixin-my>-->
  </div>
  <div v-else-if="loginType == 'workWeixin'">

<!--    <login-work-weixin style="display: flex; align-items: center;" :code="this.code" :corp-id="this.corpId"></login-work-weixin>-->
  </div>
</template>
<script>
import loginDingtalk from '../../system/login_dingtalk.vue'
import Normal_login from "../../system/normal_login.vue";
import loginWorkWeixin  from "../../system/login_workWeixin.vue";
import loginWorkWeixinMy  from "../../system/login_workWeixinMy.vue";
export default {
  extends: elmVueBase,
  components: {
    Normal_login,
     loginDingtalk,
    loginWorkWeixin,
    loginWorkWeixinMy
  } ,
  data() {
    return {
      loginType: null,
      key: null,
      code : null,
      corpId: null
    }
  },
 async  mounted() {
   // console.log(top.location.href = elm.settings.routefix+elm.settings.loginPath)
    let loginType = this.$route.query.loginType;
    this.key= this.$route.query.key
     if (elm.info.user) {
       this.step = 2.5
       this.userInfo = elm.info.user;
       location.href = elm.settings.routefix + elm.info.system.path;
       return
     }
    if (loginType && loginType == 'dingtalk') {
      this.loginType = loginType;
      return
    }
     if (loginType && loginType == 'workWeixinMy') {
       this.loginType = loginType;
       this.code= this.$route.query.code

       if (this.code) {
         this.weixinLoginMy()
         return
       }
       let res = await elm.post("/system/login/work/weixin/my/redirectUrl?key="+this.key)
       if (res == null) {
         this.$message.error('系统错误请，联系管理员')
         return
       }
       location.href = res.url
       return
     }
    if (loginType && loginType == 'workWeixin') {
      this.loginType = loginType;
      this.corpId = this.$route.query.corpid
      this.code= this.$route.query.code
      console.log(222222222222222222)
      if (this.code) {
        console.log(333333333333333333)
        this.weixinLogin()
        return
      }
        this.weixinDoRedirectUrl(this.corpId)
        return

    }
    loginType = null;

  },
  methods: {

   async weixinDoRedirectUrl(corpId) {
      console.log('corpId:',this.corpId)
      let res = await elm.post("/system/login/work/weixin/callback/getAgentId?corpId="+this.corpId)
      console.log(JSON.stringify(res))
      debugger
      if (res == null) {
        this.$message.error('系统错误请，联系管理员')
        return
      }
      let url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wwaedb5704f3d17213&redirect_uri=http%3A%2F%2Fwww.snbg365.com%2Fv%2Fsystem%2Flogin%3FloginType%3DworkWeixin%26%26corpid%3D%24CORPID%24&response_type=code&scope=snsapi_userinfo&agentid='+res.value+'&state=STATE#wechat_redirect'
      this.$message.error(url)
      console.log(url)
      location.href= url
    },
    weixinLoginMy() {
      try{

        let res = elm.post('/system/login/work/weixin/my/login?code='+this.code+"&key="+this.key, {})
       // this.res = JSON.stringify(res)
        this.preLogin(res)
      } catch (e) {
        this.$message.error(e.message)
      }
    },
    async weixinLogin() {
      try{

        let res = await elm.post('/system/login/work/weixin/callback/login?code='+this.code, {})
        //this.res = JSON.stringify(res)
        this.preLogin(res)
      } catch (e) {
        this.$message.error(e.message)
      }
    },
    async preLogin(res) {
     console.log('res: ',res)
      console.log(444444444444)
      if (res.success == true) {
        console.log(55555555555555)
        this.step = 7
        debugger
        if (this.loginType == 'workWeixin') {
          console.log(666666666666666)

          if (res.user == null || res.user.name == '') {
            console.log(77777777777777)
            if (res.user.id) {
              elm.dialog.open(this, "/v/system/init_name", "请填写您的用户名及昵称", 500, 400, {id: res.user.id});
              return
            }
            this.$message.error("用户信息不存在，请联系管理员")

          }
        }
        elm.info.token = res.token;
        // const parts = this.model.id.split(".");
        elm.info.tenant =this.key;

        elm.info.system = res.system;
        elm.info.menu = res.menu;
        elm.info.user = res.user;
        elm.info.org = res.org;
        elm.info.role = res.role;
        location.href = elm.settings.routefix + elm.info.system.path;
        //记录日志
        let oper_log = {};
        oper_log.oper_type = "2"; //钉钉
        oper_log.user_name = res.user.name;
        oper_log.nick_name = res.user.name;
        oper_log.menu_title = "/system/login/dingtalk/auth";
        oper_log.oper_time = new Date().toLocaleString();
        oper_log.msg = "登录";
        await elm.post('/system/login/log/Upsert', oper_log);
      } else if (res.success == false || res.status == 401) {
        this.model.msg = "用户名或密码错误。";
        // top.location.href = elm.settings.routefix+elm.settings.loginPath;
      } else {
        this.model.msg = "登录失败，请稍后重试。";
        // top.location.href = elm.settings.routefix+elm.settings.loginPath;
      }
      }

    }


};
</script>

